#include "Text.h"
#include "Factory.h"
#include "TextureManager.h"

Text::Text()
{
	
}

//MAKE SURE THAT FONT TEX ARE 512x512px AND 16x16 CELLS
void Text::Draw()
{
	//to work out which cell to draw 
	// u = cell num % num cells x
	// v = cell num / num cells y

	assert(m_size);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	TheTextureManager::Instance()->DrawTexture(m_texPath);

	//Vec2f position = m_pos;
	Vec2f uvPos;
	float charScreenSize = (m_size / 100.0f);

	glBegin(GL_QUADS);
	for(unsigned int n = 0; n < m_str.size(); ++n)
	{
		char c = m_str[n];

		static const float CELLDIMS = 1.0f / 16.0f;

		//16x16 cells - 512x512px - 512/16=32 = dimensions
		uvPos.X = c % 16 * CELLDIMS;
		//16x16 cells - 512x512px - 512/16=32 = dimensions
		uvPos.Y = c / 16 * CELLDIMS;

		float gap = -charScreenSize/2.0f;

		//top left uv
		glTexCoord2f(uvPos.X, uvPos.Y);
		//top left coord
		glVertex2f(m_pos.X + (n*charScreenSize) + (n*gap), m_pos.Y);

		//bottom left uv
		glTexCoord2f(uvPos.X, uvPos.Y + CELLDIMS);
		//bottom left coord
		glVertex2f(m_pos.X + (n*charScreenSize) + (n*gap), m_pos.Y - charScreenSize);

		//bottom right uv
		glTexCoord2f(uvPos.X + CELLDIMS, uvPos.Y + CELLDIMS);
		//bottom right coord
		glVertex2f(m_pos.X + (n*charScreenSize) + charScreenSize + (n*gap), m_pos.Y - charScreenSize);

		//top right uv
		glTexCoord2f(uvPos.X + CELLDIMS, uvPos.Y);
		glVertex2f( m_pos.X + (n*charScreenSize) + charScreenSize + (n*gap), m_pos.Y);
	}
	glEnd();
	glDisable(GL_BLEND);
}

bool Text::Load(File* const f)
{
	return true;
}

const char* Text::GetTypeName()
{
	return "Text";
}

GUIObject* CreateNewText()
{
	return new Text;
}

static bool text = TheGUIObjectFactory::Instance()->AddToMap("Text", CreateNewText);